/**
 * Created by dukes on 2016/1/5.
 */
class MoneyCatPanel extends eui.Component{
    //todo 招财猫的手
    public constructor(){
        super();
        this.skinName = "pet.MoneyCatSkin";
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAdded, this );
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoved, this);
    }
    private curConfig:MoneyCatAcitivityConfig;
    public updateDisplay(){
        this.curConfig = ActivityMgr.getInstance().getCurMoneyCatConfig();
        var need = this.curConfig.diamond - ActivityMgr.getInstance().moneyCatInfo.totalCharge;
        if( need > 0){
            this.group0.visible = true;
            this.img_0.source = RES.getRes("moneycat_json.money_1");
            this.group0.text = need+"";
            //GetUIAssetsNum(need,this.group0,1.0,true,false,null,"preload_json.num_1_");
        }else{
            this.group0.visible = false;
            this.img_0.source = RES.getRes("moneycat_json.money_2");
        }
        this.group1.text = this.curConfig.awardMin+"~"+this.curConfig.awardMax;
        //GetUIAssetsNum(this.curConfig.awardMin+"~"+this.curConfig.awardMax,this.group1,1.0,true,false,null,"preload_json.num_1_");
    }
    private onGet(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        this.onClose(null);
        var need = this.curConfig.diamond - ActivityMgr.getInstance().moneyCatInfo.totalCharge;
        if( need > 0){
            RemindPanel.getInstance().showContent("还需要充值"+need+"个钻石才能进行招财哦~是否前往充值界面？",this, this.gotoRecharge);
        }else{
            window["__moneyCatAward"](this.curConfig.grade);
        }
    }
    private gotoRecharge(){
        //SceneMgr.getInstance().switchScene(SceneMgr.SCENE_RECHARGE);
        BuyDiamondPanel.getInstance().show();
    }
    private onAdded(evt:egret.Event){
        this.img_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
        this.img_get.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGet, this);
        if( ActivityMgr.getInstance().moneyCatInfo)
            this.updateDisplay();
    }

    private onRemoved(evt:egret.Event){
        this.img_close.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
        this.img_get.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onGet, this);
    }

    public show(){
        DisplayController.instance().addToAlert(GameConst.GrayMask);
        DisplayController.instance().addToAlert(this);
    }
    private onClose(evt:egret.TouchEvent=null){
        if( evt )
            SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        DisplayController.instance().removeFromAlert(GameConst.GrayMask);
        DisplayController.instance().removeFromAlert(this);
    }
    public hand:eui.Image;
    public img_get:eui.Image;
    public img_close:eui.Image;
    public img_0:eui.Image;
    public group0:eui.BitmapLabel;
    public group1:eui.BitmapLabel;

    public partAdded(name:string, instance:any) {
        super.partAdded(name, instance);
        if (name == "hand") {
            this.hand = instance;
        } else if (name == "img_get") {
            this.img_get = instance;
        } else if (name == "img_close") {
            this.img_close = instance;
        } else if (name == "img_0") {
            this.img_0 = instance;
        } else if (name == "group0") {
            this.group0 = instance;
        }else if (name == "group1") {
            this.group1 = instance;
        }else if (name == "img_get") {
            this.img_get = instance;
        }
    }
    public measure() {
        super.measure();
        this.x = GameConst.StageW - this.measuredWidth >> 1;
        this.y = GameConst.StageH - this.measuredHeight >> 1;
    }
    private static _instance:MoneyCatPanel;
    public static getInstance():MoneyCatPanel{
        if( MoneyCatPanel._instance == null)
            MoneyCatPanel._instance = new MoneyCatPanel();
        return MoneyCatPanel._instance;
    }
}